package com.lee.mysql.controller;

import com.github.pagehelper.PageInfo;
import com.lee.mysql.pojo.Menu;
import com.lee.mysql.pojo.Role;
import com.lee.mysql.service.RoleService;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;

/**
 * 角色
 */

@RestController
@RequestMapping("/role")
@Api(tags = {"角色"})
public class RoleController {
    @Autowired
    private RoleService roleService;



    /** 新增角色 */
    @RequiresPermissions("system:role:add")
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    @ApiOperation(value = "新增角色")
    public Result<String> addRecord(Role record){
        if (StringUtils.isBlank(record.getName())){
            return new Result<>(Result.FAIL,"角色名称不能为空",null);
        }

        int i = roleService.addRecord(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"添加成功",null);
        }else {
            return new Result<>(Result.FAIL,"添加失败",null);
        }
    }


    /** 修改角色关联菜单 */
    @RequiresPermissions("system:role:menu")
    @RequestMapping(value = "/updateRoleMenus", method = RequestMethod.POST)
    @ApiOperation(value = "修改角色关联菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query"),
        @ApiImplicitParam(name = "menuIds", value = "菜单id(单个,多个逗号隔开)", paramType = "query")
    })
    public Result<Role> updateRoleMenus(String roleId,String menuIds){
        String[] s = menuIds.split(",");

        int i = roleService.updateRoleMenus(roleId, Arrays.asList(s));

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }



    /** 根据ID修改记录 */
    @RequiresPermissions("system:role:update")
    @RequestMapping(value = "/updateOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID修改记录")
    public Result<String> updateOneById(Role record){

        int i = roleService.updateOneById(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 根据ID批量删除记录 */
    @RequiresPermissions("system:role:delete")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID批量删除记录")
    @ApiImplicitParam(name = "ids", value = "id(单个,多个逗号隔开)", paramType = "query")
    public Result<String> deleteByIds(String ids){
        if (StringUtils.isBlank(ids)){
            return new Result<>(Result.FAIL,"请选择要删除的记录",null);
        }

        String[] split = ids.split(",");
        int s = roleService.checkSysRoles(Arrays.asList(split));
        if (s > 0){
            return new Result<>(Result.FAIL,"角色包含系统角色,不能删除!",null);
        }

        int i = roleService.deleteByIds(Arrays.asList(split));

        if (i > 0){
            return new Result<>(Result.SUCCESS,"删除成功",null);
        }else {
            return new Result<>(Result.FAIL,"删除失败",null);
        }
    }

    /** 根据ID查询记录 */
    @RequestMapping(value = "/queryOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID查询记录")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    public Result<Role> queryOneById(String id){

        Role record = roleService.queryOneById(id);

        return new Result<>(Result.SUCCESS,"操作成功",record);
    }


    /** 根据角色ID查询拥有菜单 */
    @RequestMapping(value = "/getRoleMenuList", method = RequestMethod.POST)
    @ApiOperation(value = "根据角色ID查询拥有菜单")
    @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query")
    public Result<List<Menu>> getRoleMenuList(String roleId){

        List<Menu> list = roleService.getRoleMenuList(roleId);

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }


    /** 查询分页数据 */
    @RequiresPermissions("system:role:view")
    @RequestMapping(value = "/queryPaging", method = RequestMethod.POST)
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query")
    })
    public Result<PageInfo<Role>> queryPaging(Role record, Integer page, Integer size) {
        if (page == null) page = 1;
        if (size == null) size = 10;

        Result<PageInfo<Role>> result = new Result<PageInfo<Role>>();
        PageInfo<Role> list = roleService.queryPaging(record, page, size);

        result.setData(list);
        result.setErrorCode(Result.SUCCESS);
        return result;
    }



    /** 查询所有角色 */
    @RequestMapping(value = "/queryAllRoles", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有角色")
    public Result<List<Role>> queryAllRoles() {

        Result<List<Role>> result = new Result<List<Role>>();
        List<Role> list = roleService.queryAllRoles();

        result.setData(list);
        result.setErrorCode(Result.SUCCESS);
        return result;
    }




}
